import 'dart:math';

import 'package:flutter/material.dart';

import '../utils/color_utils.dart';
import 'base/base_effect.dart';
import 'effect_template.dart';

class Effect30 extends EditableAnimationEffect {
  @override
  List<Color> get defaultColors {
    return [Color(0xffff0000), Color(0xffff00c6), Color(0xffa200ff)];
  }

  @override
  EffectType get effectTyp => EffectType.effect30;

  @override
  bool get enableAngle => false;

  @override
  bool get enableColorWidth => false;

  @override
  bool get enableDensity => false;

  @override
  bool get enableEditColor => true;

  @override
  bool get enableSpeed => false;

  ///获取动画的全部侦内容
  @override
  Future<List<List<List<Color>>>> getFrames({EffectOperation? operation}) {
    final random = Random();
    List<Color> colorList = getColorList(effectOperation: operation);
    List<List<List<Color>>> frames = [];

    // 颜色之间隔3秒
    const secondInterval = 3;
    // 每秒帧数量
    int secondFrame = 1000 ~/ frameDuration;
    // 总间隔帧
    int interval = secondFrame * secondInterval;
    // 总帧数量
    int allFrame = interval * colorList.length;

    int width = size.width.toInt();
    int height = size.height.toInt();

    // 白点总数
    int point = (width * height * 0.05).round();

    // 背景颜色
    for (int z = 0; z < allFrame; z++) {
      List<List<Color>> columns = [];

      final index_1 = z ~/ interval;
      final tempIndex = index_1 + 1;
      final index_2 = tempIndex >= colorList.length ? 0 : tempIndex;
      final startColor = colorList[index_1];
      final endColor = colorList[index_2];

      /// 生成过渡颜色
      final color = ColorUtils.getGradientColor(
        startColor: startColor,
        endColor: endColor,
        currentFrame: z,
        step: interval,
      );

      for (int y = 0; y < height; y++) {
        List<Color> rows = List.generate(width, (index) => color);
        columns.add(rows);
      }

      /// 白点随机位置
      List<Size> randomList = List.generate(point, (index) {
        int dx = random.nextInt(width);
        int dy = random.nextInt(height);
        return Size(dx.toDouble(), dy.toDouble());
      });

      /// 赋值白点
      for (int x = 0; x < randomList.length; x++) {
        int dx = randomList[x].width.toInt();
        int dy = randomList[x].height.toInt();
        for (int i = 0; i < columns.length; i++) {
          if (i == dx) {
            for (int j = 0; j < columns[i].length; j++) {
              if (j == dy) {
                columns[i][j] = Colors.white;
              }
            }
          }
        }
      }
      frames.add(columns);
    }

    return Future.value(frames);
  }

  @override
  bool get isFixedColorCount => false;

  @override
  RangeArgument get colorCountRange => RangeArgument(1, 6, 0);

  @override
  Size get size => const Size(24, 24);
}